package com.woniu.cloud.commentassembly.apiImpl;

import com.woniu.cloud.CommentApi;
import com.woniu.cloud.commentassembly.service.ITStoreReviewsService;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.form.InsertCommentForm;
import com.woniu.cloud.form.SelectCommentForm;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
public class CommentApiImpl implements CommentApi {


    @Resource
    private ITStoreReviewsService storeReviewsService;

    @Override
    @PostMapping("insertStoreReviews")
    @GlobalTransactional
    public Result insertStoreReviews(@RequestBody InsertCommentForm storeReviewForm) {
        log.info("添加评论->storeReviewForm:{}",storeReviewForm);
        if (null==storeReviewForm) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始添加->storeReviewForm:{}",storeReviewForm);
            return storeReviewsService.insertStoreReviews(storeReviewForm);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @PostMapping("getStoreReviewsByStoreReviewsStoreId")
    @GlobalTransactional
    public Result getStoreReviewsByStoreReviewsStoreId(@RequestBody SelectCommentForm selectCommentForm) {
        log.info("通过条件查找->selectCommentForm:{}",selectCommentForm);
        if (null==selectCommentForm) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始查询->selectCommentForm:{}",selectCommentForm);
            return storeReviewsService.getStoreReviewsBySelectCommentForm(selectCommentForm);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @DeleteMapping("deleteStoreReviewsByStoreReviewsId")
    @GlobalTransactional
    public Result deleteStoreReviewsByStoreReviewsId(Integer storeReviewsId) {
        log.info("根据id删除评论->storeReviewsId:{}",storeReviewsId);
        if (null==storeReviewsId) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始删除->storeReviewsId:{}",storeReviewsId);
            return storeReviewsService.deleteStoreReviewsByStoreReviewsId(storeReviewsId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",false);
        }
    }


}
